import { formatPrice } from "../../tools/Tools";
export const nodeTreeList = (treeList) => {
    const nodeTree = treeList.filter((item) => {
        delete item.children;
        item.children = treeList.filter((c) => {
            return c.pid === item.id;
        });
        return !item.pid;
    });
    return nodeTree;
};
/**
 * 财务报表按照报名名称/报表类型排序
 */
export function sortCpasTable(newFsData, field) {
    let info = field ? field : "name";
    if (!newFsData) {
        return;
    }
    const NAME_SORT_CFG = ["未审财务报表", "试算平衡", "审定财务"]; //一级报名名称排序枚举
    const TYPE_SORT_ORDER = {
        资产: 1,
        负债: 2,
        利润: 3,
        现流: 4,
        现流附: 5,
        "权益变动(本年)": 6,
        "权益变动(上年)": 7,
        减值准备: 8,
    }; //二级报名分类排序枚举.未审
    const SD_TYPE_SORT_ORDER = {
        资产: 1,
        负债: 2,
        利润: 3,
        现金流量表: 4,
        现金流补充资料表: 5,
        所有者权益变动表_本年: 6,
        所有者权益变动表_上年: 7,
        资产减值准备情况表: 8,
    }; //二级报名分类排序枚举.审定
    const TRIAL_TYPE_SORT_ORDER = {
        资产表_期末: 1,
        资产表_期初: 2,
        负债表_期末: 3,
        负债表_期初: 4,
        利润表_本期: 5,
        利润表_上期: 6,
        现金流量表_本期: 7,
        现金流量表_上期: 8,
        现金补充资料表_本期: 9,
        现金补充资料表_上期: 10,
        所有者权益变动表_本年: 11,
        所有者权益变动表_上年: 12,
    }; //二级报名分类排序枚举试算
    let nameResData = []; //按报表名称排序结果存储
    for (let i = 0; i < NAME_SORT_CFG.length; i++) {
        let temp = [];
        const name = NAME_SORT_CFG[i];
        for (let j = 0; j < newFsData.length; j++) {
            const item = newFsData[j];
            const tableName = item[info];
            if (tableName.includes(name)) {
                temp.push(item);
            }
        }
        nameResData.push(temp);
    }
    let typeResData = []; //按报表分类排序结果存储
    for (let i = 0; i < nameResData.length; i++) {
        let groupData = nameResData[i].slice();
        let sortCfg = TYPE_SORT_ORDER; //默认未审核排序规则
        //试算需要用自己的排序规则。因为试算需要期初/期末/本期/上期排序
        if (i === 1) {
            sortCfg = TRIAL_TYPE_SORT_ORDER;
        }
        //审定排序规则
        if (i === 2) {
            sortCfg = SD_TYPE_SORT_ORDER;
        }
        for (let key in sortCfg) {
            for (let j = 0; j < groupData.length; j++) {
                if (groupData[j][info].includes(key)) {
                    groupData[j]["index"] = sortCfg[key];
                }
            }
        }
        groupData.map((item) => {
            if (!item.index) {
                item.index = 99;
            }
        });
        let temp = [];
        if (i === 2) {
            //审定财务报表一张表/两张表排序
            let data = groupData.filter((item) => {
                return item.title.startsWith("审定");
            });
            let GSData = groupData.filter((item) => {
                return item.title.startsWith("公司");
            });
            let GHData = groupData.filter((item) => {
                return item.title.startsWith("合并");
            });
            if (data && data.length) {
                data = data.sort((a, b) => a.index - b.index);
            }
            if (GSData && GSData.length) {
                GSData = GSData.sort((a, b) => a.index - b.index);
            }
            if (GHData && GHData.length) {
                GHData = GHData.sort((a, b) => a.index - b.index);
            }
            temp = [...data, ...GSData, ...GHData];
        }
        else {
            temp = groupData.sort((a, b) => a.index - b.index);
        }
        typeResData.push(...temp);
    }
    console.log("resData", typeResData);
    return typeResData;
}
/**
 * 根据数组中的number重新排序校验公式
 */
export function sortValidtorData(array) {
    if (!array || !array.length) {
        return;
    }
    console.log(array, "array before");
    array.sort(function (a, b) {
        const aNumber = a.number.toString();
        const bNumber = b.number.toString();
        var aParts = aNumber.split("-");
        var bParts = bNumber.split("-");
        if (aParts[0] === bParts[0]) {
            if (aParts[1] && bParts[1]) {
                return parseInt(aParts[1]) - parseInt(bParts[1]);
            }
            else {
                return aNumber.localeCompare(bNumber);
            }
        }
        else {
            return parseInt(aParts[0]) - parseInt(bParts[0]);
        }
    });
    console.log(array, "array after");
    return array;
}
/**
 * 字符串省略多余字符
 * @param value
 * @param len
 */
export function ellipsis(value, len) {
    if (!value) {
        return;
    }
    if (value.length > len) {
        return value.slice(0, len) + "...";
    }
    return value;
}
export const RulesType = {
    "1": "表内校验",
    "2": "表间校验",
    "3": "与报表校验",
    "4": "交叉校验",
};
export const OPTIONS = [
    {
        label: "表内校验",
        value: "1",
    },
    {
        label: "表间校验",
        value: "2",
    },
    {
        label: "与报表校验",
        value: "3",
    },
    {
        label: "表内交叉校验",
        value: "4",
    },
];
export const RESULT_OPATIONS = [
    {
        label: "校验不通过",
        value: "1",
    },
    {
        label: "强制校验不通过",
        value: "2",
    },
    {
        label: "非强制校验不通过",
        value: "3",
    },
    {
        label: "差异数项目",
        value: "4",
    },
    {
        label: "逻辑不符项目",
        value: "5",
    },
];
export const formulaResult = (t) => {
    const verifyFormulaResult = Number(t.verifyFormulaResult);
    const beVerifiedFormulaResult = Number(t.beVerifiedFormulaResult);
    const fixedNum = verifyFormulaResult.toFixed(2) - beVerifiedFormulaResult.toFixed(2);
    const sourceNum = verifyFormulaResult - beVerifiedFormulaResult;
    if (fixedNum === 0 && sourceNum < 0.01 && fixedNum !== sourceNum) {
        return Number(sourceNum.toFixed(4));
    }
    return formatPrice(fixedNum.toFixed(2));
};
//格式化公式：将公式分为有效公式和无效公式
export const formatDataList = (data) => {
    let noFormulas = [];
    let formulars = [];
    const noneFormatText = "【无效公式】";
    if (!data || !data.length) {
        return {
            noFormulas,
            formulars,
        };
    }
    for (let i = 0; i < data.length; i++) {
        const rowData = data[i];
        const { verifyResult: groupVerifyResult } = data[i];
        if (groupVerifyResult && groupVerifyResult.length) {
            let invalidRes = []; //无效公式
            let validRes = []; //有效公式
            for (let w = 0; w < groupVerifyResult.length; w++) {
                if (!groupVerifyResult[w]["checkId"]) {
                    groupVerifyResult[w]["checkId"] = groupVerifyResult[w].number;
                }
                const { type, beVerifiedFormulaResultList, number, checkType, isForce, beVerifyFormat, verifyFormat, beVerifyIsNA, verifyIsNA, } = groupVerifyResult[w];
                groupVerifyResult[w]["checkType"] = RulesType[type];
                let childVerifyResult = [];
                let noChildVerifyResult = [];
                if (+type === 4 &&
                    beVerifiedFormulaResultList &&
                    beVerifiedFormulaResultList.length) {
                    for (let j = 0; j < beVerifiedFormulaResultList.length; j++) {
                        const { formula, verifyItem, value, result, formulaFormat, beVerifyIsNA = false, } = beVerifiedFormulaResultList[j];
                        const verifyIsNA = verifyItem?.verifyIsNA
                            ? verifyItem?.verifyIsNA
                            : false;
                        const a = `${number}-${j + 1}`;
                        console.log(checkType, "a--", RulesType[type]);
                        const obj = {
                            checkId: `${number}-${j + 1}`,
                            number: `${number}-${j + 1}`,
                            checkType: RulesType[type],
                            isForce,
                            beVerifiedFormula: formula,
                            fullFormula: verifyItem?.formula,
                            beVerifiedFormulaResult: value,
                            verifyFormulaResult: verifyItem?.value,
                            fullFormulaResult: result,
                            beVerifiedFormulaResultList,
                            beVerifyFormat: formulaFormat,
                            verifyFormat: verifyItem?.formula,
                            beVerifyIsNA,
                            verifyIsNA,
                        };
                        if (!beVerifyIsNA || !verifyIsNA) {
                            childVerifyResult.push(obj);
                        }
                        else {
                            noChildVerifyResult.push(obj);
                        }
                    }
                    if (childVerifyResult && childVerifyResult.length) {
                        groupVerifyResult[w].childVerifysResult = childVerifyResult;
                    }
                    if (noChildVerifyResult && noChildVerifyResult.length) {
                        groupVerifyResult[w].noChildVerifyResult = noChildVerifyResult;
                    }
                }
                if (+type !== 4) {
                    if (beVerifyIsNA || verifyIsNA) {
                        invalidRes.push({
                            ...groupVerifyResult[w],
                        });
                    }
                    else {
                        validRes.push({
                            ...groupVerifyResult[w],
                        });
                    }
                }
                else if (childVerifyResult && childVerifyResult.length) {
                    validRes = validRes.concat(childVerifyResult);
                }
                else if (noChildVerifyResult && noChildVerifyResult.length) {
                    invalidRes = invalidRes.concat(noChildVerifyResult);
                }
            }
            if (validRes && validRes.length) {
                formulars.push({
                    ...rowData,
                    verifyResult: validRes,
                });
            }
            if (invalidRes && invalidRes.length) {
                noFormulas.push({
                    ...rowData,
                    verifyResult: invalidRes,
                });
            }
        }
    }
    console.log(formulars, "formulars---", noFormulas);
    return {
        noFormulas,
        formulars,
    };
};
